package guiClasses;
/**
 * TCSS 360 - Project
 */
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.text.MaskFormatter;

import objectClasses.User;
import objectClasses.UserInfo;
/**
 *  Creates a registration panel that contains all of the objects needed for registering for an event.
 *  
 *  @author Abraham Santini
 */
public class RegistrationPanel extends Observable {
	/**
	 * The panel that hold the registration information
	 */
	private JPanel my_panel;
	
	private JLabel[] empty = new JLabel[7];
	
	private boolean isContestant;
	
	/**
	 * Public constructor for a Registration Panel.
	 * @param the_o_er The Observer object that will be updated.  This should
	 * be a BeWeaveFrame.
	 */
	
	public RegistrationPanel(final Observer the_o_er) 
	{
		isContestant = true;
		for(int i = 0; i < 6; i++)
		{
			JLabel temp = new JLabel();
			temp.setForeground(Color.RED);
			temp.setText("Field is empty");
			temp.setVisible(false);
			empty[i] = temp;
		}
		JLabel temp = new JLabel();
		temp.setForeground(Color.RED);
		temp.setText("Not a valid phone number");
		temp.setVisible(false);
		empty[6] = temp;
		
		addObserver(the_o_er);
		my_panel = new JPanel();
		
		GridBagConstraints g = new GridBagConstraints();
		
		my_panel.setLayout(new GridBagLayout());
		// creates the first name panel
		my_panel.add(new JLabel("First Name"), GBC.GBCGen(0, 0, 0, 0));
		final JTextField firstField = new JTextField(10);
		my_panel.add(firstField, GBC.GBCGen(0, 0, 1, 0));
		my_panel.add(empty[0], GBC.GBCGen(0, 0, 2, 0));
		// creates the last name panel
		my_panel.add(new JLabel("Last Name"), GBC.GBCGen(0, 0, 0, 1));
		final JTextField lastField = new JTextField(10);
		my_panel.add(lastField, GBC.GBCGen(0, 0, 1, 1));
		my_panel.add(empty[1], GBC.GBCGen(0, 0, 2, 1));
		// creates the email address panel
		my_panel.add(new JLabel("Email Address"), GBC.GBCGen(0, 0, 0, 2));
		final JTextField emailField = new JTextField(10);
		my_panel.add(emailField, GBC.GBCGen(0, 0, 1, 2));
		my_panel.add(empty[2], GBC.GBCGen(0, 0, 2, 2));
		// creates the user name panel
		my_panel.add(new JLabel("User Name"), GBC.GBCGen(0, 0, 0, 3));
		final JTextField userField = new JTextField(10);
		my_panel.add(userField, GBC.GBCGen(0, 0, 1, 3));
		my_panel.add(empty[3], GBC.GBCGen(0, 0, 2, 3));
	
		my_panel.add(new JLabel("Phone Number"), GBC.GBCGen(0, 0, 0, 4));
		final JFormattedTextField phone = new JFormattedTextField(createFormatter("(###) ###-####"));
		phone.setColumns(10);
		my_panel.add(phone, GBC.GBCGen(0, 0, 1, 4));
		my_panel.add(empty[6], GBC.GBCGen(0, 0, 2, 4));
		
		// creates the password panel
		my_panel.add(new JLabel("Password"), GBC.GBCGen(0, 0, 0, 5));
		final JPasswordField passField = new JPasswordField(10);
		my_panel.add(passField, GBC.GBCGen(0, 0, 1, 5));
		my_panel.add(empty[4], GBC.GBCGen(0, 0, 2, 5));
		
		my_panel.add(new JLabel("Retype Password"), GBC.GBCGen(0, 0, 0, 6));
		final JPasswordField reTypePassField = new JPasswordField(10);
		my_panel.add(reTypePassField, GBC.GBCGen(0, 0, 1, 6));
		my_panel.add(empty[5], GBC.GBCGen(0, 0, 2, 6));
		
		JButton registerButton = new JButton("Register");
		
		// action listener for registration button.
		registerButton.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent arg0) 
			{ 
				for(int i = 0; i < 7; i++)
				{
					empty[i].setVisible(false);
				}
				empty[3].setText("Field is empty");
				empty[5].setText("Field is empty");
				boolean failed = false;
				if(firstField.getText().isEmpty())
				{
					empty[0].setVisible(true);
					failed = true;
				}
				if(lastField.getText().isEmpty())
				{
					empty[1].setVisible(true);
					failed = true;
				}
				if(emailField.getText().isEmpty())
				{
					empty[2].setVisible(true);
					failed = true;
				}
				if(userField.getText().isEmpty())
				{
					empty[3].setVisible(true);
					failed = true;
				}
				if(passField.getPassword().length == 0)
				{
					empty[4].setVisible(true);
					failed = true;
				}
				if(reTypePassField.getPassword().length == 0)
				{
					empty[5].setVisible(true);
					failed = true;
				}
				if(phone.getValue() == null)
				{
					empty[6].setVisible(true);
					failed = true;
				}
				if(!failed)
				{
					try 
					{
						Map<String, String> user_map = User.readUserFile();
						if(user_map.get(userField.getText()) != null || userField.getText().equals("admin"))
						{
							empty[3].setText("Username not available");
							empty[3].setVisible(true);
							failed = true;
						}
					} 
					catch (IOException e1) 
					{
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
				String pass = new String(passField.getPassword());
				String re_pass = new String(reTypePassField.getPassword());
				if(!failed)
				{
					if(!pass.equals(re_pass))
					{
						empty[5].setText("Passwords not the same");
						empty[5].setVisible(true);
						failed = true;
					}
				}
				if(failed)
				{
					setChanged();
					notifyObservers(my_panel);
					return;
				}
				
				try 
				{
					if(isContestant){
					User.Register(userField.getText(), pass, emailField.getText(), firstField.getText(), 
							lastField.getText(), phone.getText(), 'A');
					GuiMain.setUser(new UserInfo('A', firstField.getText(), lastField.getText(),
							emailField.getText(), userField.getText(), phone.getText()));
					setChanged();
					notifyObservers(new AttendeePanel(the_o_er).getPanel());
					} else {
						User.Register(userField.getText(), pass, emailField.getText(), firstField.getText(), 
								lastField.getText(), phone.getText(), 'J');
						GuiMain.setUser(new UserInfo('J', firstField.getText(), lastField.getText(),
								emailField.getText(), userField.getText(), phone.getText()));
						setChanged();
						notifyObservers(new JudgePanel(the_o_er).getPanel());
					}
				} 
				catch (IOException e) 
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("Could not register user.  Try again");
				}
			}
			
		});
		
		my_panel.add(registerButton, GBC.GBCGen(0, 0, 0, 7));
		my_panel.add(Box.createVerticalGlue());
		my_panel.add(typeBoxPanel(), GBC.GBCGen(0, 0, 1, 7));
		
	}
	/**
	 * Return the panel.
	 * @return my_panel The panel that contains the registration information.
	 */
	public JPanel getPanel() {
		return my_panel;
	}
	
	protected MaskFormatter createFormatter(String s) 
	{
	    MaskFormatter formatter = null;
	    try {
	        formatter = new MaskFormatter(s);
	        formatter.setPlaceholderCharacter('_');
	    } catch (java.text.ParseException exc) {
	        System.err.println("formatter is bad: " + exc.getMessage());
	        System.exit(-1);
	    }
	    return formatter;
	}
	
	private JPanel typeBoxPanel(){
		JPanel temp = new JPanel();
		temp.setLayout(new GridBagLayout());
		JRadioButton constRadio = new JRadioButton("Contestant", true);
		constRadio.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				isContestant = true;
				
			}
			
		});
		JRadioButton judgeRadio = new JRadioButton("Judge");
		judgeRadio.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				isContestant = false;
				
			}
			
		});
		ButtonGroup buttonGroup = new ButtonGroup();
		buttonGroup.add(constRadio);
		buttonGroup.add(judgeRadio);
		temp.add(constRadio, GBC.GBCGen(0, 0, 0, 0));
		temp.add(judgeRadio, GBC.GBCGen(0, 0, 1, 0));
		
		
		return temp;
	}
}
